﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
using Loki.Utils;

namespace Loki.UI.Win
{
    public class PropertyBinder : Binder
    {
        public void OneWay<TControl, TVM>(TControl destination,
                                            Expression<Func<TControl, object>> destinationProperty,
                                            TVM source,
                                            Expression<Func<TVM, object>> sourceProperty,
                                            IValueConverter converter = null,
                                            object converterParameter = null)
                                            where TVM : INotifyPropertyChanged
        {
            // get property info
            PropertyInfo setPropertyDescriptor = ExpressionHelper.GetProperty(destinationProperty);

            // set property info
            PropertyInfo getPropertyDescriptior = ExpressionHelper.GetProperty(sourceProperty);

            // check in control property is editable
            if (setPropertyDescriptor.CanWrite)
            {
                base.OneWay(destination, setPropertyDescriptor, source, getPropertyDescriptior, converter, converterParameter);
            }
        }
    }
}